//
//  locationListView.swift
//  map
//
//  Created by Aoyun on 18/01/2025.
//

import SwiftUI

struct locationListView: View {
    @Environment(LocationViewModel.self) private var vm
    var body: some View {
        List {
            ForEach(vm.location) { location in
                Button {
                    vm.updateNextLocation(location: location)
                } label: {
                    listRowView(location: location)
                }.padding(.vertical, 4)
                 .listRowBackground(Color.clear)
            }
        }.listStyle(.plain)
    }
}

#Preview {
    locationListView().environment(LocationViewModel())
}

extension locationListView {
    private func listRowView(location: Location) -> some View {
        HStack {
            if let imageName = location.imageNames.first {
                Image(imageName).resizable()
                    .scaledToFit()
                    .frame(width: 45, height: 45)
                    .cornerRadius(10)
            }
            VStack(alignment: .leading) {
                Text(location.name)
                    .font(.headline)
                Text(location.cityName)
                    .font(.subheadline)
            }
        }.frame(maxWidth: .infinity, alignment: .leading)
    }
}
